#ifndef CUSTOM_LIT_PASS_INCLUDED
#define CUSTOM_LIT_PASS_INCLUDED

//#include "../ShaderLibrary/Common.hlsl"
#include "../ShaderLibrary/Surface.hlsl"
#include "../ShaderLibrary/Shadows.hlsl"
#include "../ShaderLibrary/BRDF.hlsl"
#include "../ShaderLibrary/Light.hlsl"
#include "../ShaderLibrary/GI.hlsl"
#include "../ShaderLibrary/Lighting.hlsl"

/*
TEXTURE2D(_BaseMap);
SAMPLER(sampler_BaseMap);

UNITY_INSTANCING_BUFFER_START(UnityPerMaterial)
	UNITY_DEFINE_INSTANCED_PROP(float4,_BaseColor)
	UNITY_DEFINE_INSTANCED_PROP(float4,_BaseMap_ST)
	UNITY_DEFINE_INSTANCED_PROP(float,_Cutoff)
	UNITY_DEFINE_INSTANCED_PROP(float,_Metallic)
	UNITY_DEFINE_INSTANCED_PROP(float,_Smoothness)
UNITY_INSTANCING_BUFFER_END(UnityPerMaterial)
*/



struct Attributes 
{
	float3 positionOS : POSITION;
	float3 normalOS : NORMAL;
	float2 baseUV :TEXCOORD0;
	float4 tangentOS : TANGENT;
	GI_ATTRIBUTE_DATA
	UNITY_VERTEX_INPUT_INSTANCE_ID
};

struct Varyings {
	float4 positionCS :SV_POSITION;
	float3 positionWS :VAR_POSITION;
	float3 normalWS : VAR_NORMAL;
	float2 baseUV :VAR_BASE_UV;
	float2 detailUV : VAR_DETAIL_UV;
	#if defined(_NORMAL_MAP)
		float4 tangentWS : VAR_TANGENT;
	#endif
	GI_VARYINGS_DATA
	UNITY_VERTEX_INPUT_INSTANCE_ID
};

Varyings LitPassVertex(Attributes input )
{
	Varyings output;
	UNITY_SETUP_INSTANCE_ID(input);
	UNITY_TRANSFER_INSTANCE_ID(input,output);
	TRANSFER_GI_DATA(input,output);

	float3 positionWS = TransformObjectToWorld(input.positionOS.xyz);
	output.positionCS = TransformWorldToHClip(positionWS);
	output.positionWS = positionWS;

	output.normalWS =  TransformObjectToWorldNormal(input.normalOS.xyz);
	/*
	float4 baseST = UNITY_ACCESS_INSTANCED_PROP(UnityPerMaterial,_BaseMap_ST);
	output.baseUV = input.baseUV * baseST.xy + baseST.zw;
	*/
	output.baseUV = TransformBaseUV(input.baseUV);
	output.detailUV = TransformDetailUV(input.baseUV);
	#if defined(_NORMAL_MAP)
	output.tangentWS = float4(TransformObjectToWorldDir(input.tangentOS.xyz),input.tangentOS.w);
	#endif
	return output;
}

float4 LitPassFragment(Varyings input) :SV_TARGET
{
//	return float4(1.0,1.0,0.0,1.0);
//return 0;
	UNITY_SETUP_INSTANCE_ID(input);
	/*
	float4 baseColor =  UNITY_ACCESS_INSTANCED_PROP(UnityPerMaterial,_BaseColor);
	float4 baseMap = SAMPLE_TEXTURE2D(_BaseMap,sampler_BaseMap,input.baseUV);
	float4 base = baseColor * baseMap;
	*/
	float4 base = GetBase(input.baseUV,input.detailUV);
	#if defined(_CLIPPING)
		//clip(base.a -  UNITY_ACCESS_INSTANCED_PROP(UnityPerMaterial,_Cutoff) );
		clip(base.a -  GetCutoff(input.baseUV) );
	#endif
	//base.rgb = abs(length(input.normalWS) -1 ) * 1;
	//base.rgb = normalize(input.normalWS);
	//base.rgb = abs(length(normalize(input.normalWS)) -1 ) * 10;

	Surface surface;
	//surface.normal = normalize(input.normalWS);
	#if defined(_NORMAL_MAP)
		surface.normal = NormalTangentToWorld(GetNormalTS(input.baseUV,input.detailUV), input.normalWS,input.tangentWS);
	#else 
		surface.normal = normalize(input.normalWS);
	#endif
	surface.viewDirection = normalize(_WorldSpaceCameraPos - input.positionWS);
	surface.depth = -TransformWorldToView(input.positionWS).z;
	surface.color = base.rgb;
	surface.alpha = base.a;
	surface.metallic =  GetMetallic(input.baseUV);//UNITY_ACCESS_INSTANCED_PROP(UnityPerMaterial,_Metallic);
	surface.smoothness =  GetSmoothness(input.baseUV,input.detailUV);//UNITY_ACCESS_INSTANCED_PROP(UnityPerMaterial,_Smoothness);
	surface.occlusion = GetOcclusion(input.baseUV);
	surface.position = input.positionWS;
	surface.fresnelStrength = GetFresnel(input.baseUV);
	surface.interpolatedNormal = input.normalWS;

	BRDF brdf = GetBRDF(surface);
	GI gi = GetGI(GI_FRAGMENT_DATA(input),surface,brdf);
	float3 color = GetLighting(surface,brdf,gi);
	color += GetEmission(input.baseUV);
	base = float4(color ,surface.alpha);
	return base;
}

#endif